/**
 * TreatmentRec
 * This class represents a patient's treatment record; views Vitals class
 */
package docoffice.doss;
import java.sql.ResultSet;
import java.util.logging.Logger;

import docoffice.database.DatabaseManager;

/**
 * @author George Chi
 * @version $Revision: 1.7 $
 */
public class TreatmentRec {

	/**
	 * Field treatDoc.
	 */
	private Doctor treatDoc;

	/**
	 * Field treatDate.
	 */
	private String treatDate; // date of treatment, can be written as "mm/dd/yy"

	/**
	 * Field treatTime.
	 */
	private String treatTime; // time of treatment, can be written as "hh:mm"

	/**
	 * Field symptoms.
	 */
	private String symptoms; // patient's symptoms, written as sentence fragments

	/**
	 * Field diagnosis.
	 */
	private String diagnosis; // doctor's diagnosis, written as phrase/word

	/**
	 * Field treatOrders.
	 */
	private DocOrders treatOrders;
	
    /**
     * Field id.
     */
	private int id;
	
	/**
	 * Field treatNurse.
	 */
	private MedicalEmployee treatNurse;
	
	/**
	 * Field LOG.
	 */
	private static final Logger LOG = Logger.getLogger("TreatmentRec");

	/**
	 * Field treatVitals.
	 */
	private Vitals treatVitals;
	
	/**
	 * Constructor for TreatmentRec.
	 * @param doc Doctor
	 * @param date String
	 * @param time String
	 * @param sym String
	 * @param diag String
	 * @param orders DocOrders
	 * @param nurse MedicalEmployee
	 * @param vitals Vitals
	 */
	public TreatmentRec(Doctor doc, String date, String time, String sym,  // $codepro.audit.disable largeNumberOfParameters
			String diag, DocOrders orders, MedicalEmployee nurse, Vitals vitals) {
		setTreatDoc(doc);
		setTreatDate(date);
		setTreatTime(time);
		setSymptoms(sym);
		setDiagnosis(diag);
		setTreatOrders(orders);
		setTreatNurse(nurse);
		setTreatVitals(vitals);
	}

	/**
	 * Method getTreatDate.
	
	 * @return Doctor */
	public Doctor getTreatDoc() {
		return treatDoc;
	}

	/**
	 * Method setTreatDoc.
	 * @param treatDoc Doctor
	 */
	public final void setTreatDoc(Doctor treatDoc) {
		this.treatDoc = treatDoc;
	}

	/**
	 * Method getTreatDate.
	
	 * @return String */
	public String getTreatDate() {
		return treatDate;
	}

	/**
	 * Method setTreatDate.
	 * @param treatDate String
	 */
	public final void setTreatDate(String treatDate) {
		this.treatDate = treatDate;
	}
	
	/**
	 * id getter
	
	 * @return id */
	public int getId(){
		return this.id;
	}
	
	/**
	 * id setter
	 * @param id
	 */
	public void setId(int id){
		this.id = id;
	}
	
	/**
	 * Method getTreatTime.
	
	 * @return String */
	public String getTreatTime() {
		return treatTime;
	}

	/**
	 * Method setTreatTime.
	 * @param treatTime String
	 */
	public final void setTreatTime(String treatTime) {
		this.treatTime = treatTime;
	}

	/**
	 * Method getSymptoms.
	
	 * @return String */
	public String getSymptoms() {
		return symptoms;
	}

	/**
	 * Method setSymptoms.
	 * @param symptoms String
	 */
	public final void setSymptoms(String symptoms) {
		this.symptoms = symptoms;
	}

	/**
	 * Method getDiagnosis.
	
	 * @return String */
	public String getDiagnosis() {
		return diagnosis;
	}

	/**
	 * Method setDiagnosis.
	 * @param diagnosis String
	 */
	public final void setDiagnosis(String diagnosis) {
		this.diagnosis = diagnosis;
	}

	/**
	 * Method getTreatOrders.
	
	 * @return DocOrders */
	public DocOrders getTreatOrders() {
		return treatOrders;
	}

	/**
	 * Method setTreatOrders.
	 * @param treatOrders DocOrders
	 */
	public final void setTreatOrders(DocOrders treatOrders) {
		this.treatOrders = treatOrders;
	}

	/**
	 * Method getTreatNurse.
	
	 * @return MedicalEmployee */
	public MedicalEmployee getTreatNurse() {
		return treatNurse;
	}

	/**
	 * Method setTreatNurse.
	 * @param treatNurse MedicalEmployee
	 */
	public final void setTreatNurse(MedicalEmployee treatNurse) {
		this.treatNurse = treatNurse;
	}

	/**
	 * Method getTreatVitals.
	
	 * @return Vitals */
	public Vitals getTreatVitals() {
		return treatVitals;
	}

	/**
	 * Method setTreatVitals.
	 * @param treatVitals Vitals
	 */
	public final void setTreatVitals(Vitals treatVitals) {
		this.treatVitals = treatVitals;
	}
	
	/**
	 * Method toString.
	
	 * @return String */
	public String toString() {
		return "TreatmentRec will return a patient's treatment record.";
	}
	
	/**
	 * Method createTreatmentRec.
	 * @param doctor Doctor
	 * @param date String
	 * @param time String
	 * @param symptoms String
	 * @param diagnosis String
	 * @param docorders DocOrders
	 * @param nurse MedicalEmployee
	 * @param vitals Vitals
	 */
	public void createTreatmentRec(Doctor doctor, String date, String time,
			String symptoms, String diagnosis, DocOrders docorders, 
			MedicalEmployee nurse, Vitals vitals) {
		final int docID = doctor.getId();
		final int nurseID = nurse.getId();
		final int docordersID = docorders.getId();
		final int vitalsID = vitals.getId();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "INSERT INTO treatmentrec (TreatingDocID" +
				", TreatmentDate, TreatmentTime, AttendingNurseID, DocOrdersID, " +
				"Symptoms, VitalsID" + ", Diagnosis) VALUES (" 
				+ docID + ", '" + date + "', '" + time + 
				"', " + nurseID + ", " + docordersID + ", '" + symptoms + "', " + vitalsID + 
				", '" + diagnosis + "')";
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while creating treatment record");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("TreatmentRec successfully created");
	}
	
	/**
	 * Method readTreatmentRec.
	 * @param treatment TreatmentRec
	
	 * @return TreatmentRec */
	public TreatmentRec readTreatmentRec(TreatmentRec treatment) {
		final int id = treatment.getId();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "SELECT * FROM treatmentrec WHERE TreatmentRecID = " + id;
		ResultSet rs = null;
		System.out.println(sqlQuery);
		try{
			rs = dbMgr.runReadQuery(sqlQuery);
			rs.first();
			final int docID = rs.getInt("TreatingDocID");
			final String date = rs.getString("TreatmentDate");
			final String time = rs.getString("TreatmentTime");
			final int nurseID = rs.getInt("AttendingNurseID");
			final int docordersID = rs.getInt("DocOrdersID");
			final String symptoms = rs.getString("Symptoms");
			final int vitalsID = rs.getInt("VitalsID");
			final String diagnosis = rs.getString("Diagnosis");
			
			Doctor doctor = new Doctor("", "");
			doctor.setId(docID);
			doctor = doctor.readDoctor();
			MedicalEmployee nurse = new MedicalEmployee("", "");
			nurse.setId(nurseID);
			nurse = nurse.readMedicalEmployee();
			DocOrders docorders = new DocOrders(null, null, "");
			docorders.setId(docordersID);
			docorders = docorders.readDocOrders(docorders);
			Vitals vitals = new Vitals(0);
			vitals.setId(vitalsID);
			vitals = vitals.readVitals(vitals);
			
			treatment.setDiagnosis(diagnosis);
			treatment.setSymptoms(symptoms);
			treatment.setTreatDate(date);
			treatment.setTreatDoc(doctor);
			treatment.setTreatNurse(nurse);
			treatment.setTreatOrders(docorders);
			treatment.setTreatTime(time);
			treatment.setTreatVitals(vitals);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while reading treatment info");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection(rs);
		}
		
		LOG.info("Treatment info read successfully");
		return treatment;
	}
	
	/**
	 * Method updateTreatmentRec.
	 * @param treatment TreatmentRec
	 */
	public void updateTreatmentRec(TreatmentRec treatment) {
		final int id = treatment.getId();
		final Doctor doc = treatment.getTreatDoc();
		final int docID = doc.readMedicalEmployeeID();
		final String date = treatment.getTreatDate();
		final String time = treatment.getTreatTime();
		final MedicalEmployee nurse = treatment.getTreatNurse();
		final int nurseID = nurse.readMedicalEmployeeID();
		final DocOrders orders = treatment.getTreatOrders();
		final int ordersID = orders.readDocOrdersId(orders);
		final String symptoms = treatment.getSymptoms();
		final Vitals vitals = treatment.getTreatVitals();
		final int vitalsID = vitals.readVitalsID(vitals);
		final String diagnosis = treatment.getDiagnosis();
		
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "UPDATE treatmentrec SET TreatingDocID = " + docID 
		         + ", TreatmentDate = '" + date + "', TreatmentTime = '" + time + "', "
		         + "AttendingNurseID = " + nurseID + ", DocOrdersID = " + ordersID + 
		         ", Symptoms = '" + symptoms + "', VitalsID = " + vitalsID + ", " +
		         "Diagnosis = '" + diagnosis + "' WHERE TreatmentRecID = " + id;
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while updating treatment info");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("Treatment info updated successfully");
	}
	
	/**
	 * Method deleteTreatmentRec.
	 * @param treatment TreatmentRec
	 */
	public void deleteTreatmentRec(TreatmentRec treatment){
		final int id = treatment.getId();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "DELETE FROM treatmentrec WHERE " +
				"TreatmentRecID = " + id;
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while deleting treatment info");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("Treatment info deleted successfully");
	}
	
	/**
	 * Method readTreatmentRecId.
	 * @param treatment TreatmentRec
	 * @return int
	 */
	public int readTreatmentRecId(TreatmentRec treatment){
		int id = 0;
		final Doctor doc = treatment.getTreatDoc();
		final int docID = doc.readMedicalEmployeeID();
		final String date = treatment.getTreatDate();
		final String time = treatment.getTreatTime();
		final MedicalEmployee nurse = treatment.getTreatNurse();
		final int nurseID = nurse.readMedicalEmployeeID();
		final DocOrders orders = treatment.getTreatOrders();
		final int ordersID = orders.readDocOrdersId(orders);
		final String symptoms = treatment.getSymptoms();
		final Vitals vitals = treatment.getTreatVitals();
		final int vitalsID = vitals.readVitalsID(vitals);
		final String diagnosis = treatment.getDiagnosis();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "SELECT TreatmentRecID FROM treatmentrec WHERE" +
				" TreatingDocID = " + docID + " AND TreatmentDate = '" + date + 
				"' AND TreatmentTime = '" + time + "' AND AttendingNurseID = " + nurseID
				+ " AND DocOrdersID = " + ordersID + " AND Symptoms = '" + symptoms + 
				"' AND VitalsID = " + vitalsID + " AND Diagnosis = '" + diagnosis + "'";
		System.out.println(sqlQuery);
		ResultSet rs = null;
		
		try{
			rs = dbMgr.runReadQuery(sqlQuery);
			rs.first();
			id = rs.getInt("TreatmentRecID");
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while reading treatment id");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection(rs);
		}
		
		LOG.info("Treatment id read successfully");
		return id;
	}
	
}
